package com.ruoyi.common.core.domain;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.sql.SqlUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class ColumnParam implements Serializable {
    private static final long serialVersionUID = 1L;
    /** 查询条件 = */
    public static final String QUERY_TYPE_EQ = "EQ";
    /** 查询条件 != */
    public static final String QUERY_TYPE_NE = "NE";
    /** 查询条件 > */
    public static final String QUERY_TYPE_GT = "GT";
    /** 查询条件 >= */
    public static final String QUERY_TYPE_GTE = "GTE";
    /** 查询条件 < */
    public static final String QUERY_TYPE_LT = "LT";
    /** 查询条件 <= */
    public static final String QUERY_TYPE_LTE = "LTE";
    public static final String QUERY_TYPE_IN = "IN";
    public static final String QUERY_TYPE_NOT_IN = "NOT_IN";
    /** 查询条件 like */
    public static final String QUERY_TYPE_LIKE = "LIKE";
    public static final String QUERY_TYPE_NOT_LIKE = "NOT_LIKE";
    /** 查询条件  是空 */
    public static final String QUERY_TYPE_EMPTY= "EMPTY";
    public static final String QUERY_TYPE_NOT_EMPTY = "NOT_EMPTY";
    public static final String QUERY_TYPE_SQL = "SQL";




    private String columnName;
    private String queryType;
    private String columnValue;
    private String dict;





   public ColumnParam() {

    }

    public ColumnParam(String columnName, String queryType, String columnValue) {
        this.columnName = columnName;
        this.queryType = queryType;
        this.columnValue = columnValue;
    }



    public static List<ColumnParam> getColumnQueryList(List<String> columnNames, List<String> queryTypes, List<String> columnValues) {
        List<ColumnParam> queryList = new ArrayList<>();
        if (columnNames !=null) {
            if (columnNames.size() != queryTypes.size() || columnNames.size() != columnValues.size()) {
                throw new ServiceException("搜索参数未填写完整");
            }
            for (int i = 0; i < columnNames.size(); i++) {
                //防止注入
                if (SqlUtil.isValidBySql(columnNames.get(i))) {
                    queryList.add(new ColumnParam(columnNames.get(i), queryTypes.get(i), columnValues.get(i)));
                }
            }
        }
        return queryList;
    }
}
